package com.no1ikun.view;

import com.no1ikun.service.PunishmentImp;
import com.no1ikun.service.StudentService;
import com.no1ikun.service.StudentServiceImpl;
import com.no1ikun.util.CalendarPanel;
import com.no1ikun.util.MyButton;
import com.no1ikun.util.MyJBox;
import com.no1ikun.util.MyJLabel;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class UpdatePunishment extends JPanel {
    private MyJTextField sno_edit;
    private MyJBox ptypeEdit;
    private MyJTextField timeEdit;
    private String[] title = {"学号","奖惩", "时间"};
    private PunishmentImp item=new PunishmentImp();
    private JTable table;

    private StudentService ss = new StudentServiceImpl();

    public UpdatePunishment() {
        //初始化数据
        init();
    }


    public void init() {
        this.setLayout(null);//绝对布局
        this.setVisible(false);

        this.setBounds(80, 80, 885, 452);
        this.setBackground(Color.WHITE);
        //添加文字
        MyJLabel maxText = new MyJLabel("学号", 100, 40, 100, 20, "微软雅黑", 1, 17, new Color(28, 136, 201));
        MyJLabel ptypeText = new MyJLabel("奖惩信息", 80, 25, 75, 300, "微软雅黑", 1, 17, new Color(28, 136, 201));
        MyJLabel timeText = new MyJLabel("时间", 40, 25, 110, 340, "微软雅黑", 1, 17, new Color(28, 136, 201));



        //添加编辑框
        sno_edit = new MyJTextField(200, 30, 155, 25);
        sno_edit.setBorder(new LineBorder(new Color(28, 136, 201)));
        ptypeEdit = new MyJBox(new String[]{"良","警告","记过","留校察看","开除学籍"},160, 300, 235, 25,Color.white,Color.black);
        ptypeEdit.setBorder(new LineBorder(new Color(28, 136, 201)));
        timeEdit = new MyJTextField(160, 340, 235, 25);
        // 定义日历控件面板类
        CalendarPanel calendarPanel = new CalendarPanel(timeEdit, "yyyy-MM-dd");
        calendarPanel.initCalendarPanel();
        calendarPanel.setBounds(150,90,251, 245);
        JLabel tips = new JLabel("日历面板");
        calendarPanel.add(tips);
        timeEdit.setBorder(new LineBorder(new Color(28, 136, 201)));

        //去数据库查询出数据
        Object[][] data = item.getData();
        //要一个二维数组
        DefaultTableModel defatleTable = new DefaultTableModel(data, title){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        //定义一个表格
        table = new JTable(defatleTable);
        //创建一个渲染器
        DefaultTableCellRenderer dc = new DefaultTableCellRenderer();
        //内容居中显示
        dc.setHorizontalAlignment(JLabel.CENTER);

        table.setDefaultRenderer(Object.class, dc);
        table.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int row=table.getSelectedRow();
                sno_edit.setText(((String)data[row][0]).trim());
                String[] arr=new String[]{"良","警告","记过","留校察看","开除学籍"};
                String cmp=((String)data[row][1]).trim();
                System.out.println("选中值"+cmp);
                int checkedIdx=0;
                for (int i = 0; i < arr.length; i++) {
                    if(arr[i].equals(cmp))
                        checkedIdx=i;
                    else
                        System.out.println("else");
                }
                //选中对应的奖惩
                ptypeEdit.setSelectedIndex(checkedIdx);
                System.out.println("选中index"+checkedIdx);
                System.out.println((String) data[row][1]);
                timeEdit.setText(((String) data[row][2]).trim());
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
//        //设置指定类的宽
//        table.getColumnModel().getColumn(5).setPreferredWidth(100);
//        table.getColumnModel().getColumn(6).setPreferredWidth(120);
        //添加一个滚动条
        JScrollPane js = new JScrollPane(table);
        js.setBounds(10, 70, 865, 200);


        //添加两个按钮
        MyButton select = new MyButton("查询", 380, 30, 70, 25, new Color(28, 136, 201), Color.WHITE);
        MyButton update = new MyButton("修改", 100, 400, 70, 25, new Color(28, 136, 201), Color.WHITE);
        MyButton add = new MyButton("添加", 200, 400, 70, 25, new Color(28, 136, 201), Color.WHITE);
        MyButton reset = new MyButton("重置", 300, 400, 70, 25, new Color(28, 136, 201), Color.WHITE);

        this.add(calendarPanel);
        this.add(select);
        this.add(sno_edit);
        this.add(maxText);


        this.add(update);
        this.add(reset);
        this.add(add);
        this.add(ptypeEdit);
        this.add(timeEdit);

        this.add(maxText);
        this.add(ptypeText);
        this.add(timeText);
        this.add(js);


        //查詢按鈕的监听事件
        select.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //1.接收用户要检索的数据
                String text = sno_edit.getText().trim();
                System.out.println("要检索的数据是:" + text);

                if (!"".equals(text)) {
                    //去数据库查询
                    Object[][] stus = item.selectPushBySno(text);
                    //重新完成数据渲染
                    TableModel dataModel = new DefaultTableModel(stus, title);
                    table.setModel(dataModel);

                } else {
                    Object[][] data1 = item.getData();
                    //重新完成数据渲染
                    TableModel dataModel = new DefaultTableModel(data1, title);
                    table.setModel(dataModel);
                }
            }
        });
        //监听修改按钮事件
        update.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取所有的数据
                String snoText = sno_edit.getText();
                String ptypeText = ptypeEdit.getText();
                String passwordEditText = timeEdit.getText().trim();

                PunishmentImp punish = new PunishmentImp(snoText,ptypeText,passwordEditText);
                //去完成修改操作
                int i = item.updateByNumberOrSno(punish);
                if (i > 0) {
                    JOptionPane.showMessageDialog(null, "恭喜修改成功!", "温馨提示", JOptionPane.WARNING_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(null, "修改失败", "温馨提示", JOptionPane.WARNING_MESSAGE);
                }

            }
        });
        reset.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                clearText();
            }
        });
        add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String snoText = sno_edit.getText();
                String ptypeText = ptypeEdit.getText();
                String passwordEditText = timeEdit.getText();
                System.out.println(snoText+"---"+ptypeText+"---"+passwordEditText);

                int i = item.insertPunishment(snoText,ptypeText,passwordEditText);
                if (i > 0) {
                    JOptionPane.showMessageDialog(null, "恭喜修改成功!", "温馨提示", JOptionPane.WARNING_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(null, "修改失败", "温馨提示", JOptionPane.WARNING_MESSAGE);
                }
            }
        });
    }

    public void clearText() {
        timeEdit.setText("");
    }

}
